package com.woniuxy.light_smart.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.light_smart.dto.InstallDto;
import com.woniuxy.light_smart.entity.Install;
import com.woniuxy.light_smart.entity.Role;
import com.woniuxy.light_smart.service.InstallService;
import com.woniuxy.light_smart.util.ObjectExcelView;
import com.woniuxy.light_smart.util.PageData;
import com.woniuxy.light_smart.util.Result;
import com.woniuxy.light_smart.vo.InstallVo;
import com.woniuxy.light_smart.vo.updateInstallVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 安装申请表
 */
@RequestMapping("installs")
@RestController
@Api(tags ="安装申请")
public class InstallController {
    @Autowired
    InstallService installService;
    /**
     * 模糊查询
     * @param d
     * @return
     */
    @PostMapping("query")
    @ApiOperation(value = "模糊查询",notes ="根据用户姓名和身份信息进行关键字查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InstallDto",value = "模糊查询对象",required = true,paramType = "query")
    })
    public Result sellbylike( @RequestBody InstallDto d){
        PageHelper.startPage(d.getCurrentPage(),d.getPageSize(),true);
        List<InstallVo> list = installService.selectAll(d);
        //得到pageInfo的目的就是为了统计一共有多少条数据
        PageInfo<InstallVo> pageInfo = new PageInfo<>(list);
        return Result.ok().data("list",list).data("total",pageInfo.getTotal());
    }
    /**
     * 无参差所有安装编号
     * @param ins
     * @return
     */
    @GetMapping("sel")
    public Result sel() {
        List<Install> installList = installService.sel();
        if (installList != null) {
            return Result.ok().data("list", installList);
        } else {
            return Result.error();
        }
    }
    /**
     * 下载文件
     * @param ins
     * @return
     */
    @RequestMapping(value = "/exportExcel")
    @ApiOperation(value = "下载文件",notes ="把当前表导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InstallDto",value = "下载文件对象",required = true,paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code =200,message = "成功"),
            @ApiResponse(code =401,message = "未认证"),
            @ApiResponse(code =403,message = "无权限"),
            @ApiResponse(code =404,message = "找不到"),
    })
    public ModelAndView exportExcel(@RequestBody InstallDto ins) {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            //构造列名称
            List<String> titles = new ArrayList<>();
            titles.add("用户编号"); // 1
            titles.add("用户姓名"); // 2
            titles.add("处理状态"); // 3
            titles.add("小组名称"); // 4
            titles.add("角色名称"); // 4
            titles.add("申请时间"); // 4
            titles.add("申请数量"); // 4
            titles.add("安装区域"); // 4
            titles.add("安装描述"); // 4
            dataMap.put("titles", titles);
            //得到被导出的数据
            List<InstallVo> varOList = installService.selectAll(ins);
            List<PageData> varList = new ArrayList<>();
            for (InstallVo insVO : varOList) {
                PageData vpd = new PageData();
                vpd.put("var1",insVO.getInstallId());
                vpd.put("var2",insVO.getUserName());
                vpd.put("var3",insVO.getHsStatus());
                vpd.put("var4",insVO.getTroopsName());
                vpd.put("var5",insVO.getRoleName());
                vpd.put("var6",insVO.getInstallDate());
                vpd.put("var7",insVO.getInstallNum());
                vpd.put("var8",insVO.getAreaName());
                vpd.put("var9",insVO.getInstallDesc());
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
    /**
     *新增
     * @param ins
     * @return
     */
    @PostMapping("")
    @ApiOperation(value = "添加",notes ="添加数据")
    @ApiResponses({
            @ApiResponse(code =200,message = "成功"),
            @ApiResponse(code =401,message = "未认证"),
            @ApiResponse(code =403,message = "无权限"),
            @ApiResponse(code =404,message = "找不到"),
    })
    public Result add(@RequestBody InstallDto d){
        int i = installService.insertDto(d);
        return Result.ok();
    }
    /**
     *查找一个对象
     * @param ins
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation(value = "查找",notes ="查找一个对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "安装编号",required = true,paramType = "path",dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code =200,message = "成功"),
            @ApiResponse(code =401,message = "未认证"),
            @ApiResponse(code =403,message = "无权限"),
            @ApiResponse(code =404,message = "找不到"),
    })
    public Result queryById(@PathVariable("id") Long id){
        updateInstallVo ac = installService.selectByPrimaryKey(id);
        return Result.ok().data("list",ac);
    }
    /**
     *更新对象
     * @param ins
     * @return
     */
    @PutMapping("")
    @ApiOperation(value = "修改",notes ="修改数据")
    public Result update(@RequestBody InstallDto ac){
        int i = installService.updateByPrimaryKeySelective(ac);
        return Result.ok();
    }
    /**
     *删除对象
     * @param ins
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation(value = "删除",notes ="删除一条数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "安装编号",required = true,paramType = "path",dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code =200,message = "成功"),
            @ApiResponse(code =401,message = "未认证"),
            @ApiResponse(code =403,message = "无权限"),
            @ApiResponse(code =404,message = "找不到"),
    })
    public Result update(@PathVariable("id") Long id){
        installService.deleteByPrimaryKey(id);
        return Result.ok();
    }
}